package com.scap.authen.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.scap.authen.dao.MenuDao;
import com.scap.authen.dao.SiteDao;
import com.scap.authen.dao.UserLoginDao;
import com.scap.authen.dao.UserMappingDao;
import com.scap.authen.entity.Application;
import com.scap.authen.entity.Menu;
import com.scap.authen.entity.UserLogin;
import com.scap.authen.entity.UserMapping;
import com.scap.authen.util.Encryption;
import com.scap.authen.util.JDate;
import com.scap.authen.util.ValidateField;

@Service("menuService")
@Transactional
public class MenuServiceImpl implements MenuService {
	
	@Autowired
	private MenuDao voMenuDao;
	@Autowired
	private ApplicationContext context;

	@Override
	public List<Object> getAllMenu() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Menu> getMenuByApplicationId(String vaApplicationId) {
		
		return voMenuDao.getMenuByApplicationId(vaApplicationId);
	}

	@Override
	public String insertMenu(HttpServletRequest request) {
		boolean chkResult = true;
		String vaResult = "";
		
		String voMenuId[] = request.getParameterValues("menu_id[]");
		String voMenuNameTH[] = request.getParameterValues("menu_name_th[]");
		String voMenuNameEN[] = request.getParameterValues("menu_name_en[]");
		String voMenuURL[] = request.getParameterValues("menu_url[]");
		String voMenuParentId[] = request
				.getParameterValues("menu_parent_id[]");
		String voMenuParent[] = request.getParameterValues("menu_parent[]");
		String voroutePreviousId[] = request
				.getParameterValues("menu_route_previous_id[]");
		String vorouteId[] = request.getParameterValues("menu_route_id[]");
		String vorouteParentId[] = request
				.getParameterValues("menu_route_parent_id[]");
		String vaApplicationId = request.getParameter("application_id");
		
		chkResult = voMenuDao.deleteMenu(vaApplicationId);
		
		if(chkResult){
			for (int i = 0; i < voMenuId.length; i++) {

				Menu voMenu = new Menu();
				voMenu.setVaApplicationId(vaApplicationId);
				voMenu.setVaMenuId(voMenuId[i]!=null? voMenuId[i] : "");
				voMenu.setVaMenuNameTh(voMenuNameTH[i]!=null? voMenuNameTH[i] : "");
				voMenu.setVaMenuNameEn(voMenuNameEN[i]!=null? voMenuNameEN[i] : "");
				voMenu.setVaMenuUrl(voMenuURL[i]!=null? voMenuURL[i] : "");
				voMenu.setVaMenuParentId(voMenuParentId[i]!=null? voMenuParentId[i] : "");
				voMenu.setVaMenuParent(voMenuParent[i]!=null? voMenuParent[i] : "");
				voMenu.setVaRoutePreviousId(voroutePreviousId[i]!=null? voroutePreviousId[i] : "");
				voMenu.setVaMenuRouteId(vorouteId[i]!=null? vorouteId[i] : "");
				voMenu.setVaMenuRouteParentId(vorouteParentId[i]!=null? vorouteParentId[i] : "");
				voMenu.setVnMenuSeq(i + 1);
				voMenu.setVaInsertDate(JDate.getDate());
				voMenu.setVaInsertTime(JDate.getTime());
				voMenu.setVnInsertUserId(1);

				chkResult = voMenuDao.insertMenu(voMenu);
				if(chkResult==false){
					break;
				}
			}
		}
		
		if (chkResult) {
			vaResult = "success";
		} else {
			vaResult = "fail";
		}
		return vaResult;
	}

	@Override
	public String updateMenu(List<Menu> voMenu, BindingResult result,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String deleteMenu(List<Menu> voMenu, HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}
	
	

}
